package com.witsight.platform.util.lang;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;

/**   
 * 说明：集合工具类
 * @Title: CollectionUtil.java 
 * @Package com.witsight.platform.util.lang 
 * @See: {@link CollectionUtils}
 * Copyright: Copyright (c) 2017
 * Company:sany huax witsight team by product
 * @author: penght  
 * @date: 2017年9月11日 下午2:52:54 
 * @version: V1.0
 *
 */
public abstract class CollectionUtil extends CollectionUtils {
	/**
	 * 说明：定义后能再同一类中使用static常量log
	 */
	private static final Logger log = LoggerFactory.getLogger(CollectionUtil.class);
	/**
	 * Convert the supplied array into a List. A primitive array gets converted
	 * into a List of the appropriate wrapper type.
	 * <p><b>NOTE:</b> Generally prefer the standard {@link Arrays#asList} method.
	 * This {@code arrayToList} method is just meant to deal with an incoming Object
	 * value that might be an {@code Object[]} or a primitive array at runtime.
	 * <p>A {@code null} source value will be converted to an empty List.
	 * @param source the (potentially primitive) array
	 * @return the converted List result
	 * @see ObjectUtils#toObjectArray(Object)
	 * @see Arrays#asList(Object[])
	 */
	@SuppressWarnings("unchecked")
	public static <T> List<T> arrayToList(T... objs) {
		return org.springframework.util.CollectionUtils.arrayToList(objs);
	}
	/**
	 * 
	 * 说明：数组转换List
	 * @Title: toList
	 * @See: {@link #toList(Object[])}
	 * @author penght
	 * @param array 数组
	 * @param <T> 范型
	 * @return 范型集合
	 * @date: 2016年9月1日 上午11:07:32
	 */
	public static <T> List<T> toList(T[] array){
		List<T> list = new ArrayList<T>();
		if (ObjectUtil.isEmpty(array)){
			return list;
		}
		
		CollectionUtils.addAll(list, array);
		return list;
	}
	 /**
     * @describe 依据某个字段对集合进行排序
     * @author panjun
     * @date 2017-12-27 下午3:44:47
     * @param list
     *            待排序的集合
     * @param fieldName
     *            依据这个字段进行排序
     * @param asc
     *            如果为true，是正序；为false，是倒序
     */
    public static <T> void sort(List<T> list, String fieldName, boolean asc) {
        Comparator<?> mycmp = ComparableComparator.getInstance();
        mycmp = ComparatorUtils.nullLowComparator(mycmp); // 允许null
        if (!asc) {
            mycmp = ComparatorUtils.reversedComparator(mycmp); // 逆序
        }
        Collections.sort(list, new BeanComparator<T>(fieldName, mycmp));
    }
}
